<?php
/**
 * Created by PhpStorm.
 * User: dream
 * Date: 2017/8/20
 * Time: 16:14
 */

//个人标签
class Label extends MY_Controller
{


    public function __construct()
    {
        parent::__construct();
        $this->loadHelper("url");
    }


    //显示标签信息列表
    public function index()
    {


        $this->loadModel("userlabel");
        $user_id = $this->session("user")->id;
        $offset = $this->uri->segment(4);
        if($this->isGet())
        {

            if(!empty($this->get('id')))
            {
                $this->userlabel->delete(['user_id'=>$user_id,'id'=>$this->get('id')]);
            }

            $base_url = web_url('label/index',true);
            $types = $this->userlabel->getWhere(['user_id'=>$user_id],$offset,self::PER_PAGE,true);
            $count = $this->userlabel->getRows(['user_id'=>$user_id]);

            $this->_label($base_url,$count,self::PER_PAGE,$types,true);
        }
        else
        {
            $type= $this->post("type");
            if($type=="add")
            {
                $article_type = $this->post("type_name");
                $set['label_name'] = $article_type;
                $set['ctime']=$this->time();
                $set["user_id"] = $user_id;
                $add_res=  $this->userlabel->add($set);
                $this->redirect(web_url('label/index',true));
            }else if($type=="search")
            {
                $key_word = $this->post("key_word");
                $where = ['label_name'=>$key_word,'user_id'=>$user_id];
                $types = $this->userlabel->getWhere($where,$offset,self::PER_PAGE,true);
                $view_data['links']="";
                $view_data['types'] = $types;
                $this->loadView(ADMIN.'label/label_index',$view_data);
            }
        }

    }

    public function update_type()
    {

        $new_name = $this->post("new_name");
        $id = $this->post("id");
        $user_id = $this->session("user")->id;
        $this->loadModel("userlabel");
        $this->userlabel->update(['label_name'=>$new_name],['id'=>$id,'user_id'=>$user_id]);
    }

    private function _label($base_url,$count,$per_page,$data,$show_count)
    {
        $this->loadHelper("url");
        $links_str = $this->bootstrapPagination($base_url,$count,$per_page,true);
        $view_data['types']=$data;
        $view_data['links']=$links_str;
        $this->loadView(ADMIN.'label/label_index',$view_data);
    }

}